#include "unit.h"

GLuint unit::tex;

unit::unit()
{
    //please NEVER call this
}

unit::unit(int nx, int ny, int nang, int nwidth, int nheight)
{
    //tex = ntex; //Texture integer
    ang = nang; //Angle
    x = nx;
    y = ny;
    originx = x;
    originy = y;
    width = nwidth;
    height = nheight;
    hp = 5;
    bounty = 1;
    progress = 0.0;
    slowTowerMod = 0;
    slowTowerDur = 0;
}

unit::unit(int nx, int ny, int nang, int nwidth, int nheight, int nhp, int nbounty, GLuint ntex){
    tex = ntex;
    x = nx;
    y = ny;
    originx = x;
    originy = y;
    width = nwidth;
    width = nwidth;
    hp = nhp;
    bounty = nbounty;
    progress = 0.0;
    slowTowerMod = 0;
    slowTowerDur = 0;
}

unit::~unit()
{

}

//Graphics
void unit::draw()
{
    glPushMatrix();
    if (ang == 0)
    {
        glTranslatef(x,y,0);
    } else {
        glTranslatef(x+(width/2),y+(height/2),0);
        glRotatef(ang,0,0,1);
        glTranslatef(-(width/2),-(height/2),0);
    }
    
    glBindTexture(GL_TEXTURE_2D, gettex());
    glBegin(GL_QUADS);
    glTexCoord2i(0,0); //Texture coord
    glVertex3f(0,0,0); //Point coord

    glTexCoord2i(1,0);
    glVertex3f(width,0,0);
    
    glTexCoord2i(1,1);
    glVertex3f(width,height,0);

    glTexCoord2i(0,1);
    glVertex3f(0,height,0);
    glEnd();
    glPopMatrix();
    glLoadIdentity();
}
void unit::draw(double prog)
{
    //We can now apply multipliers to the unit movement here
    if (slowTowerDur > 0)
    {
        slowTowerDur -= prog;
    }
    prog *= (1-slowTowerMod);
    //prog *= 3;
    progress += prog;
    while (progress > 1)
    {
        nextSpace();
        progress -= 1;
    }
    glPushMatrix();
    if (!path.empty())
    {
        float dx = progress * width;
        float dy = progress * height;
        switch (path.front())
        {
          case 1:
            dx*=-1;
            ang = 45;
            break;
          case 2:
            ang = 0;
            dx=0;
            break;
          case 3:
            ang = 315;
            break;
          case 4:
            dx*=-1;
            dy=0;
            ang = 90;
            break;
          case 6:
            dy=0;
            ang = 270;
            break;
          case 7:
            ang = 135;
            dx*=-1;
            dy*=-1;
            break;
          case 8:
            ang = 180;
            dx=0;
            dy*=-1;
            break;
          case 9:
            ang = 225;
            dy*=-1;
            break;
        }
        x = originx + dx;
        y = originy + dy;
    }
    if (ang == 0)
    {
        glTranslatef(x,y,0);
    } else {
        glTranslatef(x+(width/2),y+(height/2),0);
        glRotatef(ang,0,0,1);
        glTranslatef(-(width/2),-(height/2),0);
    }
    
    glBindTexture(GL_TEXTURE_2D, gettex());
    glBegin(GL_QUADS);
    glTexCoord2i(0,0); //Texture coord
    glVertex3f(0,0,0); //Point coord
    
    glTexCoord2i(1,0);
    glVertex3f(width,0,0);
    
    glTexCoord2i(1,1);
    glVertex3f(width,height,0);
    
    glTexCoord2i(0,1);
    glVertex3f(0,height,0);
    glEnd();
    glPopMatrix();
    glLoadIdentity();
}

void unit::nextSpace()
{ // Move to the next grid spot and remove 1 from the path list
    if (!path.empty())
    {
        int curdir = path.front();
        switch (curdir)
        {
          case 1:
            y = originy + height;
            x = originx - width;
            break;
          case 2:
            y = originy + height;
            break;
          case 3:
            y = originy + height;
            x = originx + width;
            break;
          case 4:
            x = originx - width;
            break;
          case 6:
            x = originx + width;
            break;
          case 7:
            x = originx - width;
            y = originy - height;
            break;
          case 8:
            y = originy - height;
            break;
          case 9:
            y = originy - height;
            x = originx + width;
            break;
        }
        path.pop_front();
        originx = x;
        originy = y;
    }
}

GLuint unit::gettex()
{
    return tex;
}
